$(document).ready(function() {
	var options = {
		title : "正在加载",
		closable : false
	};
	$("#waiting").dialog(options);
	$('#majorlist').datagrid({
		title : "专业管理",
		pagination : false,
		pageSize : 20,
		rownumbers : true,
		displayMsg : "当前第N页",
		idField : "major_name",
		singleSelect : true,
		loadMsg : "正在查询，请稍候。。。。。。",
		toolbar : [{
					text : '添加',
					iconCls : 'icon-add',
					handler : function() {
						showAddDialog();
					}
				}, '-', {
					text : '删除',
					iconCls : 'icon-remove',
					handler : function() {
						var row = $('#majorlist').datagrid('getSelected');
						if (row) {
							$.messager.confirm("确定", "确定要删除" + row.major_name
											+ "吗？", function(r) {
										if (r) {
											$.ajax({
												url : "../course/admin/courseclassmgr.do",
												type : "POST",
												data : {
													cmd : "del",
													courseclass : encodeURIComponent(row.major_name)
												},
												success : function(data,
														textStatus) {
													var result = eval('('
															+ data + ')');
													if (result.error) {
														$.messager.alert("错误",
																result.msg);
													} else {
														$.messager.alert("OK",
																"删除成功");
														window
																.setTimeout(reloadDataGrid);
													}
												}
											});
										}
									});
						}
					}
				}, '-', {
					text : '编辑',
					iconCls : 'icon-edit',
					handler : function() {
						var row = $('#majorlist').datagrid('getSelected');
						if (row) {
							showEditDialog(row.class_name);
						}
					}
				}

		]
	});
	reloadDataGrid(renderPager);
});

function reloadDataGrid(callbackFunc) {
	$.ajax({
				url : "../course/admin/courseclassmgr.do",
				type : "POST",
				data : {
					cmd : "listall"
				},
				success : function(data, textStatus) {
					var result = eval('(' + data + ')');
					$('#majorlist').datagrid("loadData", result);
					if (callbackFunc) {
						callbackFunc();
					}
				}
			});
}
function renderPager() {
	$.ajax({
				url : "../course/admin/courseclassmgr.do",
				type : "POST",
				data : {
					cmd : "count"
				},
				success : function(data, textStatus) {
					var result = parseInt(data);
					var options = {
						showPageList : false,
						showRefresh : false,
						pageSize : 40,
						pageNumber : 1,
						total : result,
						displayMsg : "第 {from} 到 {to} ",
						onSelectPage : function(pageNumber, pageSize) {
							$.ajax({
										url : "../course/admin/courseclassmgr.do",
										type : "POST",
										data : {
											cmd : "listall",
											page : pageNumber
										},
										success : function(data, textStatus) {
											var result = eval('(' + data + ')');
											$('#majorlist').datagrid(
													"loadData", result);
										}
									});
						}
					};
					$("#pager").pagination(options);
				}
			});
}